package cn.swufe.edu.stu.hw_tools.Cal;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import cn.swufe.edu.stu.hw_tools.Convert.Dis_CV_Activity;
import cn.swufe.edu.stu.hw_tools.R;

public class DepositActivity extends AppCompatActivity {
    private ArrayList<HashMap<String, String>> listItems;
    private SimpleAdapter listItemAdapter;
    private ListView listView;
    private float money = 0.0f;
    private float WXYJmoney = 0.0f;
    private float ZXmoney = 0.0f;
    private float rate1,rate2,rate3,rate4,rate5,rate6;
    private float out1,out2,out3,out4,out5,out6,out7;
    private float month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12;
    private int index;
    private int wxyj_nums,zx_nums;
    ActivityResultLauncher luncher =registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>(){
                @Override
                public void onActivityResult(ActivityResult result) {
                    Intent intent = result.getData();
                    if (result.getResultCode() == 2 && intent != null) {
                        Bundle bundle = intent.getExtras();{
                            rate1 = bundle.getFloat("rate1", 0.0f);
                            rate2 = bundle.getFloat("rate2", 0.0f);
                            rate3 = bundle.getFloat("rate3", 0.0f);
                            rate4 = bundle.getFloat("rate4", 0.0f);
                            rate5 = bundle.getFloat("rate5", 0.0f);
                            rate6 = bundle.getFloat("rate6", 0.0f);
                            wxyj_nums = judge(rate1) + judge(rate2) + judge(rate3)
                                    + judge(rate4)+ judge(rate5)+ judge(rate6);
                            HashMap<String, String> map2 = new HashMap<String, String>();
                            map2.put("kind", "五险一金" ); // 标题文字
                            map2.put("discribe", "已选择" + wxyj_nums + "项\n点击查看"); // 详情描述
                            listItems.set(1,map2);
                            listView.setAdapter(listItemAdapter);
                            WXYJmoney = Float.parseFloat(String.format("%.2f",money * (rate1 + rate2 + rate3 + rate4 + rate5 + rate6) / 100));
                        }
                    }else if (result.getResultCode() == 4 && intent != null) {
                        Bundle bundle = intent.getExtras();{
                            out1 = bundle.getFloat("out1", 0.0f);
                            out2 = bundle.getFloat("out2", 0.0f);
                            out3 = bundle.getFloat("out3", 0.0f);
                            out4 = bundle.getFloat("out4", 0.0f);
                            out5 = bundle.getFloat("out5", 0.0f);
                            out6 = bundle.getFloat("out6", 0.0f);
                            out7 = bundle.getFloat("out7", 0.0f);
                            zx_nums = judge(out1) + judge(out3) +judge(out2) +
                                    judge(out4) +judge(out5) +judge(out6) +judge(out7);
                            HashMap<String, String> map3 = new HashMap<String, String>();
                            map3.put("kind", "专项扣除" ); // 标题文字
                            map3.put("discribe", "已选择" + zx_nums + "项\n点击查看"); // 详情描述
                            listItems.set(2,map3);
                            listView.setAdapter(listItemAdapter);
                            ZXmoney = Float.parseFloat(String.format("%.2f",(out1 + out2 + out3 + out4 + out5 + out6 + out7)));
                        }
                    }else if (result.getResultCode() == 3 && intent != null) {
                            index = intent.getIntExtra("index",0);
                            HashMap<String, String> map4 = new HashMap<String, String>();
                            map4.put("kind", "选择期数" ); // 标题文字
                            map4.put("discribe", "已选择第" + index + "期\n点击查看"); // 详情描述
                            listItems.set(3,map4);
                            listView.setAdapter(listItemAdapter);
                            //cn.swufe.edu.stu.hw_tools
                    }
                }
            });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deposit);

        TextView v = findViewById(R.id.XX4textView);
        listView = findViewById(R.id.XX4list);
        initListView();
        listView.setAdapter(listItemAdapter);
        rate1 = 0.0f; rate2 = 0.0f; rate3 = 0.0f; rate4 = 0.0f; rate5 = 0.0f; rate6 = 0.0f;
        out1  = 0.0f; out2  = 0.0f; out3  = 0.0f; out4  = 0.0f; out5  = 0.0f; out6  = 0.0f; out7  = 0.0f;

        SharedPreferences sharedPreferences = getSharedPreferences("monthly", Activity.MODE_PRIVATE);
        month1  = sharedPreferences.getFloat("month1" ,0.0f);
        month2  = sharedPreferences.getFloat("month2" ,0.0f);
        month3  = sharedPreferences.getFloat("month3" ,0.0f);
        month4  = sharedPreferences.getFloat("month4" ,0.0f);
        month5  = sharedPreferences.getFloat("month5" ,0.0f);
        month6  = sharedPreferences.getFloat("month6" ,0.0f);
        month7  = sharedPreferences.getFloat("month7" ,0.0f);
        month8  = sharedPreferences.getFloat("month8" ,0.0f);
        month9  = sharedPreferences.getFloat("month9" ,0.0f);
        month10 = sharedPreferences.getFloat("month10",0.0f);
        month11 = sharedPreferences.getFloat("month11",0.0f);
        month12 = sharedPreferences.getFloat("month12",0.0f);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent config;
                if(position==1){
                    config = new Intent(DepositActivity.this, WXYJActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putFloat("rate1", rate1);
                    bundle.putFloat("rate2", rate2);
                    bundle.putFloat("rate3", rate3);
                    bundle.putFloat("rate4", rate4);
                    bundle.putFloat("rate5", rate5);
                    bundle.putFloat("rate6", rate6);
                    config.putExtras(bundle);
                    luncher.launch(config);
                }else if(position==2){
                    config = new Intent(DepositActivity.this, ZXActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putFloat("out1", out1);
                    bundle.putFloat("out2", out2);
                    bundle.putFloat("out3", out3);
                    bundle.putFloat("out4", out4);
                    bundle.putFloat("out5", out5);
                    bundle.putFloat("out6", out6);
                    bundle.putFloat("out7", out7);
                    config.putExtras(bundle);
                    luncher.launch(config);
                }else if(position==0){
                    config = new Intent(DepositActivity.this, STableActivity.class);
                    luncher.launch(config);
                }
                else if(position==3){
                    config = new Intent(DepositActivity.this, DeIndexActivity.class);
                    luncher.launch(config);
                }
            }
        });


    }

    public void back(View btn){
        if(getResources().getResourceEntryName(btn.getId()).equals("XX4back")){
            Intent intent = getIntent();
            setResult(2,intent);
            finish();
        }else if(getResources().getResourceEntryName(btn.getId()).equals("XX4Search")){
            try {
                TextView before = findViewById(R.id.XX4editText);
                money = Float.parseFloat(String.format("%.2f",Float.parseFloat(before.getText().toString())));
                TextView after = findViewById(R.id.XX4ANSView);
                String AFmoney = String.valueOf(money - WXYJmoney - ZXmoney);
                String totalMoney = String.valueOf(month1+month2+month3+month4+month5+month6+month7+month8+month9+month10+month11+month12);
                after.setText("您今年累计税后收入为" + totalMoney + "元\n本期税后收入为：" + AFmoney + "元");
                float save = Float.parseFloat(AFmoney);
                SharedPreferences sp = getSharedPreferences("monthly",Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putFloat("month"+index,save);
                editor.apply();
            } catch (Exception e) {
                Toast.makeText(this, "٩(๑òωó๑)۶输入数字啊，大哥~", Toast.LENGTH_SHORT).show();
            }
        }else if(getResources().getResourceEntryName(btn.getId()).equals("XX4reset")){
            SharedPreferences sp = getSharedPreferences("monthly",Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            for (int i = 0 ; i < 12 ; i ++ ){
                editor.putFloat("month"+(i+1),0.0f);
            }
            editor.apply();
            Toast.makeText(this, "重置成功", Toast.LENGTH_SHORT).show();
        }
    }

    private void initListView() {
        listItems = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> map1 = new HashMap<String, String>();
            map1.put("kind", "税收表" ); // 标题文字
            map1.put("discribe", "点击查看"); // 详情描述
            listItems.add(map1);
            HashMap<String, String> map2 = new HashMap<String, String>();
            map2.put("kind", "五险一金" ); // 标题文字
            map2.put("discribe", "已选择" + wxyj_nums + "项\n点击查看"); // 详情描述
            listItems.add(map2);
            HashMap<String, String> map3 = new HashMap<String, String>();
            map3.put("kind", "专项扣除" ); // 标题文字
            map3.put("discribe", "已选择" + zx_nums + "项\n点击查看"); // 详情描述
            listItems.add(map3);
            HashMap<String, String> map4 = new HashMap<String, String>();
            map4.put("kind", "选择期数" ); // 标题文字
            map4.put("discribe", "已选择第" + index + "期\n点击查看"); // 详情描述
            listItems.add(map4);
        // 生成适配器的Item和动态数组对应的元素
        listItemAdapter = new SimpleAdapter(this, listItems, // listItems数据源
                R.layout.list_tem2, // ListItem的XML布局实现
                new String[] { "kind", "discribe" },
                new int[] { R.id.kind, R.id.discribe}
        );
    }

    public int judge(float num){
        if(num != 0.0f){
            return 1;
        }else {
            return 0;
        }
    }

}